<?php
/**
 * AOP切面编程之验证
 * Created by PhpStorm.
 * User: Egor <http://gitee.com/seosite>
 * Date: 2018/4/11
 * Time: 下午11:09
 */

namespace WAdmin\Controller;

use Think\Controller;

class CheckFactoryController extends Controller
{

    public static function isLogin()
    {
        $checkFamily = new checkFamily();
        $checkFamily->addCheck(new is_login());
        return $checkFamily;
    }


    public static function isParamSet()
    {
        $checkFamily = new checkFamily();
        $checkFamily->addCheck(new isParamSet());
        return $checkFamily;
    }


    public static function isChildParamSet()
    {
        $checkFamily = new checkFamily();
        $checkFamily->addCheck(new isChildParamSet());
        return $checkFamily;
    }

    public static function inletSet()
    {
        $checkFamily = new checkFamily();
        $checkFamily->addCheck(new isInletParamSet());
        return $checkFamily;
    }

}

/**
 * Interface Extension
 * @package App\Controller
 */
interface Extension
{
    public function before($param);

    public function end($param);
}

/**
 * check admin user
 * Class is_admin
 * @package App\Controller
 */
class is_admin implements Extension
{
    public function before($param)
    {
    }

    public function end($param)
    {
    }
}


/**
 * check user login
 * Class is_login
 * @package App\Controller
 */
class is_login implements Extension
{
    private $userInfo;

    public function __construct()
    {
        $userInfo = decode_token();
        $this->userInfo = empty($userInfo) ? "" : (object)$userInfo;
    }

    public function before($param)
    {
        $api = new ApiController();
        if (!array_key_exists('token',get_post_vars())) return $api->output(showErrorMsg('token is not set!',100002),I('post.'));
        if (empty($this->userInfo)) return $api->output(showErrorMsg('user token check failure!',100004), I('post.'));
        return true;
    }

    public function end($param)
    {
    }
}


/**
 * check param
 * Class isParamSet
 * @package App\Controller
 */
class isParamSet implements Extension{

    public function before($param)
    {
        $api = new ApiController();
        if (!array_key_exists('param',get_post_vars())) return $api->output(showErrorMsg('param is not set!',100002),I('post.'));
        return true;
    }

    public function end($param)
    {
    }
}


/**
 * check param child
 * Class isChildParamSet
 * @package App\Controller
 */
class isChildParamSet implements Extension{

    public function before($param)
    {
        $api = new ApiController();

        if (empty($param)) return $api->output(showErrorMsg('param child is null!',100002),I('post.'));

        $paramArr = explode(',',$param);
        foreach ($paramArr as &$value){
            if (!array_key_exists($value,get_param_vars())) return $api->output(showErrorMsg($value.' is not set!',100002),I('post.'));
        }
        unset($value);

        return true;
    }

    public function end($param)
    {
    }
}

class isRoleModel implements Extension
{
    private $userInfo;

    public function __construct()
    {
        $userInfo = decode_token();
        $this->userInfo = empty($userInfo) ? "" : (object)$userInfo;
    }

    public function before($param)
    {
        // TODO: Implement before() method.

        if (!empty($this->userInfo)){
            $role_id = M('Admin')->where(['id'=>$this->userInfo->id])->getField('role_id');

            $nav_ids = M('access')->where(['role_id'=>$role_id])->getField('nav_id');

            $nav_id_arr = explode('#',$nav_ids);

            $nav_id_str = implode(',',$nav_id_arr);



        }
    }

    public function end($param)
    {
        // TODO: Implement end() method.
    }
}

class isInletParamSet implements Extension{

    public function before($param)
    {
        $api = new ApiController();

        if (empty($param)) return $api->output(showErrorMsg('param is null!',100002),I('post.'));

        $paramArr = explode(',',$param);
        foreach ($paramArr as &$value){
            if (!array_key_exists($value,get_post_vars())) return $api->output(showErrorMsg($value.' is not set!',100002),I('post.'));
        }
        unset($value);

        return true;
    }

    public function end($param)
    {
    }
}

/**
 * Class checkFamily
 * @package App\Controller
 */
class checkFamily implements Extension
{

    private $arr = array();

    public function addCheck(Extension $extension)
    {
        $this->arr[] = $extension;
    }

    public function before($param)
    {
        foreach ($this->arr as $extension) {
            $extension->before($param);
        }
    }

    public function end($param)
    {
        foreach ($this->arr as $extension) {
            $extension->end($param);
        }
    }
}